//
//  ZJDesThirdMorePhotoViewController.m
//  BreadTravel
//
//  Created by 张健 on 15/12/25.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZJDesThirdMorePhotoViewController.h"
#import "ZJDesThirdPhotoDetailViewController.h"
#import "ZJDesThirdMorePhotoModel.h"
#import "ZJDesThirdMorePhotoCollectionViewCell.h"
@interface ZJDesThirdMorePhotoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,retain)NSMutableArray *photoArray;
@property(nonatomic,retain)UICollectionView *collectionView;

@end

@implementation ZJDesThirdMorePhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置滚动方向
    //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置每个item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH/3-5, WIDTH/3-5);
    //设置最小行间距
    flowLayout.minimumLineSpacing = 5;
    //设置最小列间距
    flowLayout.minimumInteritemSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.92 alpha:1];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ZJDesThirdMorePhotoCollectionViewCell class] forCellWithReuseIdentifier:@"ZJDesThirdMorePhotoCollectionViewCell"];
    
#pragma mark - 初始化
    self.photoArray = [NSMutableArray array];
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/photos/?start=0&count=21&gallery_mode=true",self.type,self.id];
    [AFNetworkTool getUrl:url body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = result[@"items"];
        for (NSMutableDictionary *dic in array) {
            ZJDesThirdMorePhotoModel *photoModel = [ZJDesThirdMorePhotoModel modelWithDictionary:dic];
            [self.photoArray addObject:photoModel];
        }
        [self.collectionView reloadData];
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}



//每个cell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"ZJDesThirdMorePhotoCollectionViewCell";
    ZJDesThirdMorePhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.photo_Url = [[self.photoArray objectAtIndex:indexPath.row] photo_s];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.photoArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZJDesThirdPhotoDetailViewController *photoDVC = [[ZJDesThirdPhotoDetailViewController alloc]init];
    photoDVC.i = indexPath.row;
    photoDVC.photoArray = [NSMutableArray arrayWithArray:self.photoArray];
    [self presentViewController:photoDVC animated:YES completion:^{
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
